unit appMappldXmlGroup;

interface

uses  classes, sysutils, contnrs
    , NativeXml
    , maps
    , appMappldGroup
    , xmlObject
    , appMappldXmlNode
    , appMappldTypes
    , appMappldXmlLayer
;

type

    TMapMappldXmlGroups = class;

{------------------------------ TMappldXmlGroup }

    {***************************************************************************
    * TMappldXmlGroup
    ***************************************************************************}
    TMappldXmlGroup = class( TMappldXmlObject )
private
    Fgroup                      : TMappldGroup;
    Flayers                     : TMappldXmlLayers;

private
    procedure   setGroup( aGroup : TMappldGroup );

    procedure   internalRuntime2Xml();
    procedure   internalXml2Runtime();

public
    procedure   AfterConstruction(); override;

    procedure   runtime2xml();
    procedure   xml2runtime();
    procedure   updateXml( aNode : TXmlNode );

public //property
    property code      : string  index 0 read getStringParameter write setStringParameter;
    property name      : string  index 1 read getStringParameter write setStringParameter;
    property flVisible : boolean index 2 read getBooleanParameter write setBooleanParameter;
    property flShow    : boolean index 3 read getBooleanParameter write setBooleanParameter;
    property flInfo    : boolean index 4 read getBooleanParameter write setBooleanParameter;

public //property
    property group  : TMappldGroup read Fgroup write setGroup;
    property layers : TMappldXmlLayers read Flayers;

    end;

{------------------------------ TMappldXmlGroups }

    {***************************************************************************
    * TMappldXmlGroups
    ***************************************************************************}
    TMappldXmlGroups = class( TMappldXmlObject )
private
    FxmlGroups                  : TMapMappldXmlGroups;
    Fgroups                     : TMapMappldGroups;

private
    procedure   setGroups( aGroups : TMapMappldGroups );

public
    procedure   AfterConstruction(); override;
    destructor  Destroy(); override;

    procedure   runtime2xml();
    procedure   xml2runtime();
    procedure   updateXml( aNode : TXmlNode );
    procedure   getUpdatedParams( aList : TObjectList );

public //property
    property groups    : TMapMappldGroups read Fgroups write setGroups;
    property xmlGroups : TMapMappldXmlGroups read FxmlGroups;

    end;

{------------------------------ TMapMappldGroups }

    {***************************************************************************
    * TMapMappldGroups
    ***************************************************************************}
    TMapMappldXmlGroups = class( TMapBase )
private
    procedure   setItemByIndex( aIndex : integer; const aMappldXmlGroup : TMappldXmlGroup );
    procedure   setItemByKey( const aKey : string; const aMappldXmlGroup : TMappldXmlGroup );
    function    getItemByIndex( aIndex : integer ) : TMappldXmlGroup;
    function    getItemByKey( const aKey : string ) : TMappldXmlGroup;

public
    procedure   AfterConstruction(); override;

public
    property items[ aIndex : integer]          : TMappldXmlGroup read getItemByIndex write setItemByIndex;
    property itemsByKey[ const aKey : string ] : TMappldXmlGroup read getItemByKey write setItemByKey; default;

    end;

implementation

{------------------------------ TMapMappldXmlGroups }

{*******************************************************************************
* AfterConstruction
*******************************************************************************}
procedure TMapMappldXmlGroups.AfterConstruction();
begin
    flOwnsObjects := true;
end;

{*******************************************************************************
* getItemByIndex
*******************************************************************************}
function TMapMappldXmlGroups.getItemByIndex( aIndex : integer ) : TMappldXmlGroup;
begin
    Result := TMappldXmlGroup( Objects[aIndex] );
end;

{*******************************************************************************
* getItemByKey
*******************************************************************************}
function TMapMappldXmlGroups.getItemByKey( const aKey : string ) : TMappldXmlGroup;
begin
    Result := TMappldXmlGroup( ObjectsByKey[ aKey ] );
end;

{*******************************************************************************
* setItemByIndex
*******************************************************************************}
procedure TMapMappldXmlGroups.setItemByIndex( aIndex : integer; const aMappldXmlGroup : TMappldXmlGroup );
begin
    Objects[ aIndex ] := aMappldXmlGroup;
end;

{*******************************************************************************
* setItemByKey
*******************************************************************************}
procedure TMapMappldXmlGroups.setItemByKey( const aKey : string; const aMappldXmlGroup : TMappldXmlGroup );
begin
    ObjectsByKey[ aKey ] := aMappldXmlGroup;
end;

{------------------------------ TMappldXmlGroups }

{*******************************************************************************
* AfterConstruction
*******************************************************************************}
procedure TMappldXmlGroups.AfterConstruction();
begin
    inherited;

    FxmlNode.Name := XML_A_groups;
    //setOwnNode( XML_A_groups ); //, XML_C_ddTools_Array );

    FxmlGroups := TMapMappldXmlGroups.Create();
end;

{*******************************************************************************
* Destroy
*******************************************************************************}
destructor TMappldXmlGroups.Destroy();
begin
    FreeAndNil( FxmlGroups );
end;

{*******************************************************************************
* setGroups
*******************************************************************************}
procedure TMappldXmlGroups.setGroups( aGroups : TMapMappldGroups );
var
    i         : integer;
    xmlGroup  : TMappldXmlGroup;
    arrayNode : TMappldXmlNode;

begin
    Fgroups   := aGroups;
    arrayNode := TMappldXmlNode( FxmlNode ); //TMappldXmlNode( addObjectNode( XML_A_array, XML_C_array ) );

    for i := 0 to aGroups.Count - 1 do
    begin
        xmlGroup := TMappldXmlGroup.Create( arrayNode.addObject() );
        xmlGroup.group := aGroups.items[i];

        FxmlGroups[ aGroups.keys[i] ] := xmlGroup;
    end;

    //runtime2xml();
end;

{*******************************************************************************
* updateXml
*******************************************************************************}
procedure TMappldXmlGroups.updateXml( aNode : TXmlNode );
//var
    //i    : integer;
    //node : TXmlNode;

begin
    if ( aNode.Name <> XML_A_groups ) then
    begin
        exit;
    end;

    // TODO
end;

{*******************************************************************************
* runtime2xml
*******************************************************************************}
procedure TMappldXmlGroups.runtime2xml();
var
    i : integer;

begin
    for i := 0 to Fgroups.Count - 1 do
    begin
        FxmlGroups.items[i].runtime2xml();
    end;
end;

{*******************************************************************************
* xml2runtime
*******************************************************************************}
procedure TMappldXmlGroups.xml2runtime();
var
    i : integer;

begin
    for i := 0 to FxmlGroups.Count - 1 do
    begin
        FxmlGroups.items[i].xml2runtime();
    end;
end;

{*******************************************************************************
* getUpdatedParams
*******************************************************************************}
procedure TMappldXmlGroups.getUpdatedParams( aList : TObjectList );
var
    i : integer;

begin
    for i := 0 to FxmlGroups.Count - 1 do
    begin
        FxmlGroups.items[i].getUpdatedParams( aList );
    end;
end;

{------------------------------ TMappldXmlGroup }

{*******************************************************************************
* AfterConstruction
*******************************************************************************}
procedure TMappldXmlGroup.AfterConstruction();
begin
    inherited;

    FxmlNode.Name := XML_A_group;

    addStringValue( XML_A_code, '', true );
    addStringValue( XML_A_name, '', true );
    addBooleanValue( XML_A_flVisible, false, true );
    addBooleanValue( XML_A_flShow, false, true );
    addBooleanValue( XML_A_flInfo, false, true );

    Flayers := TMappldXmlLayers.Create( addObjectNode() );
end;

{*******************************************************************************
* bindToMappl
*******************************************************************************}
procedure TMappldXmlGroup.setGroup( aGroup : TMappldGroup );
begin
    Fgroup := aGroup;
    Flayers.layers := aGroup.layers;

    internalRuntime2Xml();
end;

{*******************************************************************************
* updateXml
*******************************************************************************}
procedure TMappldXmlGroup.updateXml( aNode : TXmlNode );
var
    i    : integer;
    node : TXmlNode;

begin
    if ( aNode.Name <> XML_A_group ) then
    begin
        exit;
    end;

    for i := 0 to aNode.NodeCount - 1 do
    begin
        node := aNode.Nodes[i];

        if ( node.Name = XML_A_name ) then
        begin
            name := node.ValueAsString;
        end
        else if ( node.Name = XML_A_flVisible ) then
        begin
            flVisible := node.ValueAsBoolDef( false );
        end
        else if ( node.Name = XML_A_flShow ) then
        begin
            flShow := node.ValueAsBoolDef( false );
        end
        else if ( node.Name = XML_A_width ) then
        begin
            flInfo := node.ValueAsBoolDef( false );
        end;
    end;
end;

{*******************************************************************************
* internalRuntim2Xml
*******************************************************************************}
procedure TMappldXmlGroup.internalRuntime2Xml();
begin
    code      := Fgroup.code;
    name      := Fgroup.name;
    flVisible := Fgroup.flVisible;
    flShow    := Fgroup.flShow;
    flInfo    := Fgroup.flInfo;
end;

{*******************************************************************************
* internalXml2Runtime
*******************************************************************************}
procedure TMappldXmlGroup.internalXml2Runtime();
begin
    Fgroup.name      := name;
    Fgroup.flVisible := flVisible;
    Fgroup.flInfo    := flInfo;
end;

{*******************************************************************************
* runtime2xml
*******************************************************************************}
procedure TMappldXmlGroup.runtime2xml();
begin
    internalRuntime2Xml();
    Flayers.runtime2xml();
end;

{*******************************************************************************
* xml2Runtime
*******************************************************************************}
procedure TMappldXmlGroup.xml2Runtime();
begin
    internalXml2Runtime();
    Flayers.xml2Runtime();
end;

end.
